var productTable;
var index;
var url;
var arr = new Array();
var uploadImg;
layui.use(['table','form','layer','jquery','upload'], function() {
    var table = layui.table
    var form = layui.form
    var layer = layui.layer
    var $ = layui.jquery
    var upload = layui.upload

    var userid = $("#usrid").html();

    // 初始table
    productTable = table.render({
        elem: "#productTable"
        , url: "/proOrder/find"
        ,where: {userId: userid}
        , page: {limit: 10, limits: [5, 10, 15]}
        , cellMinWidth: 80
        , toolbar: '#toolbarProduct'
        , cols: [[ //表头
            {type: 'checkbox', fixed: 'left'}
            , {field: 'orderId', title: '单号', fixed: 'center', hide: true}
            , {field: 'productName', title: '商品名称', fixed: 'center'}
            , {field: 'price', title: '单价', fixed: 'center'}
            , {field: 'count', title: '数量', fixed: 'center'}
            , {fixed: 'right', title: '操作', toolbar: '#barProduct', width: 250}
        ]]
    })

    //复选框监听
    table.on('checkbox(productTable)', function(data){
        var checked = data.checked
        var sun = data.data.price * data.data.count;
        if (checked){//勾选时
            arr.push(data.data.orderId)
            sun = $("#l1").html() - 0  +sun
            $("#l1").html(sun)
        }else{
            console.log(arr[0] == data.data.orderId)
            arr.remove(data.data.orderId)
            for(var i = 0;i++;i<arr.length){
                console.log(arr[i] == data.data.orderId)
                if (arr[i] == data.data.orderId){
                    arr[i] = null;
                }
            }
            sun = $("#l1").html() - sun
            $("#l1").html(sun)
        }
        console.log(arr)
    });

    //监听头部工具栏事件
    table.on('toolbar(productTable)',function(obj) {
        if(obj.event=="productDelChk"){
            batchdel(obj)
        }
    })

    //监听行工具事件
    table.on('tool(productTable)', function(obj){
        var data = obj.data
        if(obj.event == "edit"){
            edit(data)
        }else  if(obj.event == "del"){
            del(data)
        }
    })

    //批量删除
    function batchdel(obj) {
        var checkStatus = table.checkStatus("productTable")
        var data = checkStatus.data;
        if (data.length==0){
            layer.msg("请至少选中一个！")
        }else{
            var msg ="";
            var array = new Array();
            $.each(data , function(index , item){
                array.push(item.orderId)
            })
            layer.confirm("您确定要删除这"+ array.length +"条数据么?",function(ind) {
                $.post("/proOrder/del", {"orderId":array} ,function(d) {
                    layer.msg(d.msg)
                    // 刷新table数据
                    productTable.reload()
                })
                layer.close(ind)
            })
        }
    }

    //删除
    function del(data){
        var msg ="";
        var array = new Array();
        array.push(data.orderId);
        layer.confirm("您确定要从购物车中移除这条数据么?",function(ind) {
            $.post("/proOrder/del", {"orderId":array} ,function(d) {
                layer.msg(d.msg)
                // 刷新table数据
                productTable.reload()
            })
            layer.close(ind)
        })
    }

    //打开修改菜单
    function edit(data) {
        index =  layer.open({
            type:1 // 1（页面层）
            , title:"修改数量"
            , content:$("#saveOrUpdateDiv")
            , area:["500px","250px"]
            , success:function(layero, index) {
                // 把当前行的数据data显示到form表单中去,form表单中是有id隐藏字段
                form.val("dataFrm",data)

                // contents = layedit.build('content');
                // urls="/news/edit"  // 设置修改的地址
            }
        })
    }

    //添加/修改表单提交
    form.on("submit(doSubmit)",function() {
        // 获取页面上输入的内容
        var params = $("#dataFrm").serializeArray();
        console.log(params)
        var count = params[0].value;
        $.post("/proOrder/update",$("#dataFrm").serialize(),function(data) {
            // 刷新table数据
            productTable.reload()
            setTimeout(function(){
                layer.closeAll();
            },100);
        })

    })
})


//支付
function pay() {
    var id = $("#usrid").html();
    var money = $("#l1").html();
    arr.push(id)
    arr.push(money)
    $.post("/proOrder/toTakeOutOrder", {"arr":arr} , function(data) {
        console.log(data.code)
        layer.msg("支付成功！")
        // 刷新table数据
        productTable.reload()
        setTimeout(function(){
            layer.closeAll();
        },300);
    })
    arr.splice(arr.length-2,arr.length);
}

//数组删除方法
Array.prototype.indexOf = function(val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};
Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};